package com.lxmes.sales.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;

import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.lxmes.sales.domain.NcSafetyStock;
import com.lxmes.sales.service.INcSafetyStockService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 安全库存管理总Controller
 *
 * @author ruoyi
 * @date 2023-04-14
 */
@Api(tags = "安全库存管理总")
@RestController
@RequestMapping("/sales/safetyStock")
public class NcSafetyStockController extends BaseController
{
    @Autowired
    private INcSafetyStockService ncSafetyStockService;

    /**
     * 查询安全库存管理总列表
     */
    @ApiOperation("查询安全库存管理总列表")
    @PreAuthorize("@ss.hasPermi('sales:safetyStock:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcSafetyStock ncSafetyStock)
    {
        startPage();
        List<NcSafetyStock> list = ncSafetyStockService.selectNcSafetyStockList(ncSafetyStock);
        return getDataTable(list);
    }

    /**
     * 导出安全库存管理总列表
     */
    @ApiOperation("导出安全库存管理总列表")
    @PreAuthorize("@ss.hasPermi('sales:safetyStock:export')")
    @Log(title = "安全库存管理总", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcSafetyStock ncSafetyStock)
    {
        List<NcSafetyStock> list = ncSafetyStockService.selectNcSafetyStockList(ncSafetyStock);
        ExcelUtil<NcSafetyStock> util = new ExcelUtil<NcSafetyStock>(NcSafetyStock.class);
        util.exportExcel(response, list, "安全库存管理总数据");
    }

    /**
     * 获取安全库存管理总详细信息
     */
    @ApiOperation("获取安全库存管理总详细信息")
    @PreAuthorize("@ss.hasPermi('sales:safetyStock:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncSafetyStockService.selectNcSafetyStockById(id));
    }

    /**
     * 新增安全库存管理总
     */
    @ApiOperation("新增安全库存管理总")
    @PreAuthorize("@ss.hasPermi('sales:safetyStock:add')")
    @Log(title = "安全库存管理总", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcSafetyStock ncSafetyStock)
    {
        return toAjax(ncSafetyStockService.insertNcSafetyStock(ncSafetyStock));
    }

    /**
     * 修改安全库存管理总
     */
    @ApiOperation("修改安全库存管理总")
    @PreAuthorize("@ss.hasPermi('sales:safetyStock:edit')")
    @Log(title = "安全库存管理总", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcSafetyStock ncSafetyStock)
    {
        return toAjax(ncSafetyStockService.updateNcSafetyStock(ncSafetyStock));
    }

    /**
     * 删除安全库存管理总
     */
    @ApiOperation("删除安全库存管理总")
    @PreAuthorize("@ss.hasPermi('sales:safetyStock:remove')")
    @Log(title = "安全库存管理总", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncSafetyStockService.deleteNcSafetyStockByIds(ids));
    }

    /**
     * 导入安全库存管理总
     */
    @Log(title = "安全库存管理总", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcSafetyStock> util = new ExcelUtil<>(NcSafetyStock.class);
        List<NcSafetyStock> ncSafetyStockList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncSafetyStockService.imporIteminfo(ncSafetyStockList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcSafetyStock> util = new ExcelUtil<>(NcSafetyStock.class);
        util.importTemplateExcel(response,"安全库存管理总");
    }
}
